package com.forum.mapper;

import com.forum.annotation.AutoFill;
import com.forum.dto.ThreadUpdateDTO;
import com.forum.dto.ThreadsPageQueryDTO;
import com.forum.entity.Threads;
import com.forum.enumeration.OperationType;
import com.forum.vo.UserInfoStatisticsVO;
import com.forum.vo.ThreadsVO;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface ThreadsMapper {
    Page<Threads> pageQuery(ThreadsPageQueryDTO threadsPageQueryDTO);

    @Update("update threads set is_sticky = #{isSticky} where id=#{id} ")
    void updateByIdIsSticky(Long id, Integer isSticky);

    @Delete("delete from threads where id = #{id}")
    void deleteById(Long id);

    @Select("select count(*) from threads where category_id = #{id}")
    Long queryByCategoryId(Long id);

    void updateByIdIsLocked(ThreadUpdateDTO threadUpdateDTO);

    @Select("select * from threads where id = #{id}")
    Threads queryById(Long id);

    @AutoFill(value = OperationType.INSERT)
    void publish(Threads threads);

    @Select("select * from threads t left join images i " +
            "on t.id = i.thread_id  where type = #{type} " +
            "order by create_time desc " +
            "limit 3")
    List<ThreadsVO> queryTop3(Integer type);

    @Select("select * from threads where type = #{type} order by be_like_count desc limit 4")
    List<Threads> queryTop4(Integer type);

    @Select("select * from threads where type = #{type} order by be_like_count desc limit 2")
    List<Threads> queryArticleTop3(Integer type);

    List<Threads> queryHotArticles(Integer type, Long categoryId);

    List<Threads> latestArticle(Integer type, Long categoryId);

    @Select("select * from threads where user_id = #{userId} order by be_like_count desc ")
    List<Threads> queryByUserId(Long userId);

    @Select("select COUNT(*) AS releaseCount, SUM(be_like_count) AS likeCount from threads where user_id = #{id}")
    UserInfoStatisticsVO releaseLikeCount(Long id);
}
